package com.example.app.webview

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.collection.ArrayMap
import com.example.app.base.BaseFragment
import com.example.app.databinding.FCommonWebBinding


/**
 * 作者: Joker
 * 时间: 2019/1/19. 15:11
 * 描述: web页面
 */
class CommonWebF : BaseAgentWebFragment<FCommonWebBinding>() {

    companion object {
        fun startWeb(fragment: BaseFragment, url: String?, params: ArrayMap<String, Any?>? = null): CommonWebF {
            val webF = CommonWebF()
            val commonParamMap = WebURL.getCommonParam()
            var paramsStr = if (url?.contains("?") == true) "&" else "?"
            if (!params.isNullOrEmpty()) {
                commonParamMap.putAll(params)
            }
            commonParamMap.forEach { (k, v) -> paramsStr = "$paramsStr$k=$v&" }

            webF.arguments = Bundle().apply {
                putString("url", "${url}${paramsStr.dropLast(1)}")
            }
            fragment.start(webF)
            return webF
        }
    }

    private var url: String? = null

    override fun getViewBinding(inflater: LayoutInflater) = FCommonWebBinding.inflate(inflater)
    override fun getUrl() = url ?: ""
    override fun getWebContentView() = mViewBinding!!.flContent

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.apply {
            url = getString("url")
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewBinding?.apply {
        }
    }

    override fun onDestroy() {
        mViewBinding?.apply {
        }
        super.onDestroy()
    }
}